﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace SmartSpace.Core
{
    public class CoreContext : DomainContext
    {
        public CoreContext(DbContext dbContext)
            : base(dbContext)
        {

        }

        public static void Build(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ImageFile>().ToTable("__ImageFile");
            modelBuilder.Entity<ImageFile>().HasKey(p => p.Id);

            modelBuilder.Entity<User>().ToTable("__User");
            modelBuilder.Entity<User>().HasKey(p => p.Id);
            modelBuilder.Entity<User>().Property(p => p.Id).IsRequired();

            modelBuilder.Entity<Role>().ToTable("__Role");
            modelBuilder.Entity<Role>().HasKey(p => p.Id);
            modelBuilder.Entity<Role>().Property(p => p.Id).IsRequired();

            modelBuilder.Entity<RelationRoleToUser>().ToTable("__RelationRoleToUser");
            modelBuilder.Entity<RelationRoleToUser>().HasKey(p => p.Id);
            modelBuilder.Entity<RelationRoleToUser>()
                .HasRequired<Role>(p => p.Role).WithMany().HasForeignKey(p => p.RoleId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<RelationRoleToUser>()
                .HasRequired<User>(p => p.User).WithMany().HasForeignKey(p => p.UserId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<RelationRoleToUser>()
                .HasRequired<User>(p => p.Assigner).WithMany().HasForeignKey(p => p.AssignerId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Application>().ToTable("__Application");
            modelBuilder.Entity<Application>().HasKey(p => p.Id);
            modelBuilder.Entity<Application>().Property(p => p.Id).IsRequired();

            modelBuilder.Entity<Theme>().ToTable("__Theme");
            modelBuilder.Entity<Theme>().HasKey(p => p.Id);

            modelBuilder.Entity<ApplicationModule>().ToTable("__ApplicationModule");

            modelBuilder.Entity<Module>().ToTable("__Module");
            modelBuilder.Entity<Module>().HasKey(p => p.Id);
            modelBuilder.Entity<Module>().HasMany(p => p.Views);

            modelBuilder.Entity<ModuleView>().ToTable("__ModuleView");
            modelBuilder.Entity<ModuleView>().HasKey(p => p.Id);

            modelBuilder.Entity<Navigation>().ToTable("__Navigation");
            modelBuilder.Entity<Navigation>().HasKey(p => p.Id);
            modelBuilder.Entity<Navigation>()
                .HasMany(p => p.Children)
                .WithOptional()
                .HasForeignKey(p => p.ParentId);

            modelBuilder.Entity<Shortcut>().ToTable("__Shortcut");
            modelBuilder.Entity<Shortcut>().HasKey(p => p.Id);

            modelBuilder.Entity<Org>().ToTable("__Org");
            modelBuilder.Entity<Org>().HasKey(p => p.Id);
            modelBuilder.Entity<Org>().Property(p => p.Id).IsRequired();
            modelBuilder.Entity<Org>().HasMany(p=>p.Children)
                .WithOptional().HasForeignKey(p => p.ParentId);

            modelBuilder.Entity<OrgMember>().ToTable("__OrgMember");
            modelBuilder.Entity<OrgMember>().HasKey(p => p.Id);
            modelBuilder.Entity<OrgMember>().HasRequired<User>(p => p.User)
                .WithMany().HasForeignKey(p => p.UserId);
            modelBuilder.Entity<OrgMember>().HasRequired<Org>(p => p.Org)
                .WithMany().HasForeignKey(p => p.OrgId);

            modelBuilder.Entity<RoleNavigation>().ToTable("__RoleNavigation");
            modelBuilder.Entity<RoleNavigation>().HasKey(p => p.Id);
        }
    }
}
